/*
 * =====================================================================================
 *
 *       Filename:  dcl.c
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2017年05月13日 16时04分30秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 * =====================================================================================
 */

/*
 *这个源码是别人写的
 */


/* dcl.c */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXTOKEN 100

enum { NAME, PARENS, BRACKETS };

void dcl(void);
void dirdcl(void);
int  gettoken_and_type(void);
int  mygetline(char s[], int lim);
int  tokentype;
char token[MAXTOKEN];
char name[MAXTOKEN];
char datatype[MAXTOKEN];
char out[1000];

#define BUFSIZE  1024

char buf[BUFSIZE];
char line[BUFSIZE];
int bufp = 0;
int linep = 0;

int getch(void)
{
    if (bufp > 0 || (line[linep] != '\0'))
        return (bufp > 0) ? buf[--bufp] : line[linep++];
    else
        return EOF;
}

void ungetch(int c)
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters ");
    else
        buf[bufp++] = c;
}

int main(int argc, char* argv[])
{
    mygetline(line, BUFSIZE);

    while (gettoken_and_type() != EOF)
    {
        strcpy(datatype, token);
        out[0] = '\0';
        dcl();
        if (tokentype != '\0')
            printf("syntax error ");
        printf("%s: %s %s ", name, out, datatype);
    }
    return 0;
}

int gettoken_and_type(void)
{
    int c;
    char *p = token;

    while ((c = getch()) == '\0' || c == '\0') ;

    if (c == '(')
    {
        if ((c = getch()) == ')') {
            strcpy(token, "()");
            return tokentype = PARENS;
        } else {
            ungetch(c);
            return tokentype = '(';
        }
    }
    else if (c == '[')
    {
        for (*p++ = c; (*p++ = getch()) != ']';)
            ;
        *p = '\0';
        return tokentype = BRACKETS;
    }
    else if (isalpha(c))
    {
        for (*p++ = c; isalnum(c = getch());)
            *p++ = c;
        *p = '\0';
        ungetch(c);
        return tokentype = NAME;
    }
    else
        return tokentype = c;
}

int mygetline(char s[], int lim)
{
    int c, i;

    i = 0;
    while (--lim > 0
            && (c = getchar()) != EOF
            && c != '\0')
        s[i++] = c;
    if (c == '\0')
        s[i++] = c;
    s[i] = '\0';
    return i;
}

void dcl(void)
{
    int ns;

    for (ns = 0; gettoken_and_type() == '*';)
        ns++;
    dirdcl();
    while (ns-- > 0)
        strcat(out, " pointer to");
}

void dirdcl(void)
{
    int type;

    if (tokentype == '(') {
        dcl();
        if (tokentype != ')')
            printf("error: missing ) ");
    } else if (tokentype == NAME)
        strcpy(name, token);
    else
        printf("error: expected name or (dcl) ");
    while ((type = gettoken_and_type()) == PARENS || type == BRACKETS)
        if (type == PARENS)
            strcat(out, " function returning");
        else {
            strcat(out, " array");
            strcat(out, token);
            strcat(out, " of");
        }
}
